const {as4KDotsBaseInfo} = require("../db/init");
const {Op} = require("sequelize");
const {isPureEnglish, isPureChinese, isPureNumber, containsEnglishOrAsterisk} = require("../utils/string_utils");
const {whereName} = require("../utils/stock_search_utils");

/**
 * 选出流通股中机构较多的个股,且筹码集中
 * @param offset
 * @param limit
 * @returns {Promise<Model[]>}
 */
module.exports.findListCentralize = async function (offset = 0, limit = 10) {

    return await as4KDotsBaseInfo.findAll({
        order: [
            ['holder_total_num', 'asc'],
            ['freehold_ratio_total', 'desc']
        ],
        attributes: [
            'id',
            'stock_code',
            'stock_name',
            'hold_focus',
            'ave_price',
            'holder_total_num',
            'freehold_ratio_total',
            'tc_count',
            'jj_count',
        ],
        offset: offset,
        limit: limit,
        where: {
            ave_price: {
                [Op.lt]: 20
            },
            jj_count: {
                [Op.gt]: 5
            },
            hold_focus: {
                [Op.like]: '%集中%'
            },
            [Op.and]: [
                {
                    stock_name: {
                        [Op.notLike]: '%st%',
                    }
                },
                {
                    stock_name: {
                        [Op.notLike]: '%退%',
                    }
                }
            ]
        }
    });
}

/**
 * 查询指定股票的基本信息
 * @param stock_code
 * @returns {Promise<*>}
 */
module.exports.findOne = async function (stock_code) {
    let where = whereName(stock_code)
    console.log('where',where)
    return await as4KDotsBaseInfo.findOne({
        where: where
    });
}


/**
 * 获取指定题材的公司列表
 * @param offset
 * @param limit
 * @param tc_name 题材名字,表中题材的名字是所有题材的拼接
 * @returns {Promise<{count: *, rows: *}>}
 */
module.exports.findAllByBoardName = async function (offset, limit, tc_name) {
    let {count, rows} = await as4KDotsBaseInfo.findAndCountAll({
        where: {
            tc_name: {
                [Op.like]: `%${tc_name}%`
            },
            hold_focus: {
                [Op.like]: '%集中%'
            }
        },
        offset: parseInt(offset), limit: parseInt(limit)
    });

    return {count, rows}
}


/**
 * 获取风险股票
 * @param stock_code 风险名称
 * @param offset
 * @param limit
 * @returns {Promise<any>}
 */
module.exports.findListRisk = async function (stock_code, offset = 0, limit = 10) {
    let where = {}
    console.log('risk_name', stock_code)
    if (stock_code !== undefined && stock_code !== '') {
        where = whereName(stock_code)
    }
    where.danger_type = {
        [Op.not]: '0'
    }
    let {count, rows} = await as4KDotsBaseInfo.findAndCountAll({
        order: [
            ['holder_total_num', 'asc'],
            ['freehold_ratio_total', 'desc']
        ],
        attributes: [
            'id',
            'stock_code',
            'stock_name',
            'hold_focus',
            'ave_price',
            'holder_total_num',
            'freehold_ratio_total',
            'tc_count',
            'jj_count',
            'danger_type',
        ],
        offset: offset,
        limit: limit,
        where: where
    });
    return {count, rows}
}
